package com.alibaba.citrus.cr.sales.plan.facade.service.setting;

import com.alibaba.citrus.cr.common.annotation.FacadeInvoker;
import com.alibaba.citrus.cr.sales.plan.api.setting.StrategyFactorWriteService;
import com.alibaba.citrus.cr.sales.plan.dto.api.request.StrategyFactorRequest;
import com.alibaba.citrus.cr.sales.plan.functions.salesplan.StrategyFactorEntity;
import com.alibaba.citrus.cr.sales.plan.repository.StrategyFactorRepository;
import com.alibaba.citrus.cr.sales.plan.sdo.StrategyFactorSDO;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Objects;

@Service
public class StrategyFactorWriteServiceImpl implements StrategyFactorWriteService {

    @Resource
    private StrategyFactorRepository strategyFactorRepository;

    @FacadeInvoker
    @Override
    public Result<String> createStrategyFactor(StrategyFactorSDO strategyFactorSDO) {
        if (Objects.isNull(strategyFactorSDO)) {
            return Result.fail("", "系统维护中");
        }
        StrategyFactorEntity strategyFactorEntity = StrategyFactorEntity.of(strategyFactorSDO);
        String id = strategyFactorEntity.doCreate();
        return Result.success(id);
    }

    @FacadeInvoker
    @Override
    public Result<Integer> editStrategyFactorState(StrategyFactorSDO strategyFactorSDO) {
        StrategyFactorRequest strategyFactorRequest = new StrategyFactorRequest();
        strategyFactorRequest.setId(String.valueOf(strategyFactorSDO.getId()));

        StrategyFactorSDO byId = strategyFactorRepository.queryStrategyFactor(strategyFactorRequest);
        if (strategyFactorSDO.getStatus() != null) {
            byId.setStatus(strategyFactorSDO.getStatus());
        }

        StrategyFactorEntity strategyFactorEntity = StrategyFactorEntity.of(byId);
        Integer count = strategyFactorEntity.doEdit();
        return Result.success(count);
    }
}
